#include"CentralCache.h"


CentralCache CentralCache::_sInst;


// 从central cache中获取若干数量的内存对象
size_t CentralCache::FetchRangeObject(void*& start, void*& end, size_t batchNum, size_t size)
{
    size_t index = SizeClass::Index(size);

    _spanLists[index]._mtx.lock();

    Span* span = GetOneSpan(_spanLists[index], size);
    assert(span);
    assert(span->_freeList);

    start = span->_freeList;
    end = start;
    size_t i = 0;
    size_t actualNum = 1;
    while(i < batchNum - 1 && NextObj(end) != nullptr)
    {
        end = NextObj(end);
        i++;
        actualNum++;
    }

    _spanLists[index]._mtx.unlock();

    return actualNum;
}


// 从SpanList或者page cache中获取非空的span
Span* CentralCache::GetOneSpan(SpanList& list, size_t size)
{
    Span* it = list.Begin();

    while(it != list.End())
    {
        if(it->_freeList != nullptr)
        {
            return it;
        }
        else
        {
            it = it->_next;
        }
    }

    // 走到这说明需要从page cache中获取内存

    // 先解除桶锁, 
}